from settings import *
from run_log import *
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction
import time,arrow

from pymongo import MongoClient

xhs_client = MongoClient(mongodb["localhost"])  # 链接到腾讯云还是本地
xhs_db = xhs_client["xhs_db"]
xhs_tab_share = xhs_db["xhs_tab_share"]


class PoBase:

    def __init__(self, desired_capabilities, DRIVER_SERVER):
        self.desired_capabilities = desired_capabilities
        self.DRIVER_SERVER = DRIVER_SERVER
        self.driver = webdriver.Remote(self.DRIVER_SERVER, desired_capabilities=self.desired_capabilities)
        self.driver.implicitly_wait(3)
        self.width = self.driver.get_window_size()['width']
        self.height = self.driver.get_window_size()['height']
        logger.info("appium服务器连接成功:\n\t屏幕尺寸：{}*{}，\n\t端口地址：{}，\n\t设备名称：{}".format(self.height, self.width, self.DRIVER_SERVER,self.desired_capabilities["deviceName"]))

    def elm_select(self, *loc):
        elm = self.driver.find_element(*loc)
        logger.debug('元素被找到')
        return elm

    def elms_select(self, *loc):
        elms = self.driver.find_elements(*loc)
        logger.debug('{}个元素被找到'.format(len(elms)))
        return elms

    def elm_wait_select(self, timeout=3, *loc):
        elm = WebDriverWait(self.driver, timeout, poll_frequency=0.2).until(lambda x: x.find_element(*loc))
        logger.debug('元素被找到')
        return elm

    def elms_wait_select(self, timeout=3, *loc):
        elms = WebDriverWait(self.driver, timeout, poll_frequency=0.2).until(lambda x: x.find_elements(*loc))
        logger.debug('{}个元素被找到'.format(len(elms)))
        return elms

    def back(self, interval=0.2, num=1):
        """[点击手机的返回按钮]

        Args:
            interval (number): [点击的间隔和等待时间]. Defaults to 0.5.
            num (int): [连续点击的次数]. Defaults to 1.
        """
        while num > 0:
            time.sleep(interval)
            self.driver.keyevent(4)
            logger.debug("点击手机实体返回按钮")
            num -= 1

    def press(self, x, y):
        TouchAction(self.driver).tap(x=x, y=y).release().perform()

    def swipe_x(self, start, stop, time=1000):
        self.driver.swipe(self.width * 0.5, self.height * start, self.width * 0.5, self.height * stop, time)
        swipe_len = start - stop
        if swipe_len > 0:
            logger.debug('下 滑屏幕{}评'.format(abs(swipe_len)))
        else:
            logger.debug('上 横滑屏幕{}评'.format(abs(swipe_len)))

    def swipe_y(self, start=0.4, stop=0.1, time=200):
        self.driver.swipe(self.width * start, self.height * 0.5, self.width * stop, self.height * 0.5, time)
        swipe_len = start - stop
        if swipe_len > 0:
            logger.debug('r->l 横滑屏幕{}评'.format(abs(swipe_len)))
        else:
            logger.debug('l->r 横滑屏幕{}评'.format(abs(swipe_len)))

    def get(self, url):
        self.driver.get(url)

    def get_screenshot_as_file(self, path="./screenshot/"):
        path = "{}{}.png".format(path, time.time())
        self.driver.get_screenshot_as_file(path)
        logger.debug('截屏{}'.format(path))

    def quit(self):
        self.driver.quit()
        logger.debug('杀死APP')


def main():
    app = PoBase(desired_capabilities=VIVO)


if __name__ == '__main__':
    main()